﻿Public Class frmHLV
    Dim ctx As New QLDBDataContext
    Dim hlv As New dsHLV
    Dim clb As New dsCLB
    Dim qgia As New dsQuocGia
    Dim tk As New TimKiem
    Dim ten As String
    Dim mahlv As Integer
    Dim qg As Integer

    Private Sub frmHLV_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Tạo giao diện cho gridview
        If dgvHLV.DataSource Is Nothing Then
            CaiDatGridView()
        End If


        'Load gridview
        dgvHLV.DataSource = hlv.LayDuLieuHLV()

        'Load cbDoiBong co ALL
        cbDoiBongTK.DataSource = clb.LayDoiBongTK()
        cbDoiBongTK.DisplayMember = "TenCLB"
        cbDoiBongTK.ValueMember = "MaCLB"

        'Load cbQuocGia them
        cbQuocGia.DataSource = qgia.LayQuocTichTK()
        cbQuocGia.DisplayMember = "TenQT"
        cbQuocGia.ValueMember = "MaQT"

        'Load cbQuocGia co ALL
        cbQuocGiaTK.DataSource = qgia.LayQuocTichTK()
        cbQuocGiaTK.DisplayMember = "TenQT"
        cbQuocGiaTK.ValueMember = "MaQT"
        'Load clb cbo duoi
 


    End Sub

    Public Sub CaiDatGridView()


        With dgvHLV
            .ColumnCount = 5
            .ColumnHeadersDefaultCellStyle.ForeColor = Color.White
            .ColumnHeadersDefaultCellStyle.BackColor = Color.Navy
            .ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.Pink

            .GridColor = Color.Black
            .BackgroundColor = Color.Gray
            .ForeColor = Color.Green
            .AlternatingRowsDefaultCellStyle.ForeColor = Color.Purple

            .Columns(0).Name = "STT"
            .Columns(0).DataPropertyName = "MaHLV"
            .Columns(0).Width = 40
            .Columns(1).Name = "Họ tên"
            .Columns(1).Width += 48
            .Columns(1).DataPropertyName = "TenHLV"
            .Columns(2).Name = "Ngày sinh"
            .Columns(2).DataPropertyName = "NgaySinh"
            .Columns(3).Name = "Quốc tịch"
            .Columns(3).DataPropertyName = "QuocTich"
            .Columns(4).Name = "Đội bóng"
            .Columns(4).DataPropertyName = "TenCLB"
        End With


    End Sub

    Private Sub btnLamLai_Click(sender As Object, e As EventArgs) Handles btnLamLai.Click
        Me.Cursor = Cursors.AppStarting
        txtTK.Clear()
        cbDoiBongTK.SelectedValue = 0
        cbQuocGiaTK.SelectedValue = 0
        Me.Cursor = Cursors.Default
        txtTK.Focus()
    End Sub

    Public Function BuildSearchQuery() As IQueryable(Of HUANLUYENVIEN)

        'Dim ten As String
        ten = txtTK.Text
        'Dim mahlv As Integer
        mahlv = tk.LaymaHLVtheoCLB(cbDoiBongTK.SelectedValue)
        'Dim qg As Integer
        qg = cbQuocGiaTK.SelectedValue
        'Dim query As IQueryable(Of HUANLUYENVIEN) = ctx.HUANLUYENVIENs
        'If txtTK.Text.Length > 0 Then
        '    query = query.Where(Function(p) p.TenHLV.Contains(txtTK.Text))
        'End If
        'If tk.LaymaHLVtheoCLB(cbDoiBongTK.SelectedValue) > 0 Then
        '    query = query.Where(Function(p) p.MaHLV = tk.LaymaHLVtheoCLB(cbDoiBongTK.SelectedValue))
        'End If
        'If cbQuocGiaTK.SelectedValue > 0 Then
        '    query = query.Where(Function(p) p.MaQT = cbQuocGiaTK.SelectedValue)
        'End If
        Dim query As IQueryable(Of HUANLUYENVIEN) = tk.TKCLB(ten, mahlv, qg)
        Return query
    End Function

    Private Sub btnTimKiem_Click(sender As Object, e As EventArgs) Handles btnTimKiem.Click
        Me.Cursor = Cursors.AppStarting
        'Dim ten As String
        ten = txtTK.Text
        'Dim mahlv As Integer
        mahlv = tk.LaymaHLVtheoCLB(cbDoiBongTK.SelectedValue)
        'Dim qg As Integer
        qg = cbQuocGiaTK.SelectedValue

        Dim query As IQueryable(Of HUANLUYENVIEN) = tk.TKCLB(ten, mahlv, qg)
        numberOfpage = Math.Ceiling(query.Count / pagesize)
        curpage = 1
        lblTrang.Text = String.Format("{0}/{1}", curpage, numberOfpage)
        dgvHLV.DataSource = query.Skip((curpage - 1) * pagesize).Take(pagesize)
        isDirty = False
        SetNavigationState()
        '  dgvHLV.DataSource = tk.TKCLB(ten, mahlv, qg)

    End Sub
    'Phan trang
    Dim pagesize As Integer = 10
    Dim curpage As Integer
    Dim numberOfpage As Integer
    Dim isDirty As Boolean
    Sub SetNavigationState()
        btnToi.Enabled = (curpage < numberOfpage)
        btnCuoi.Enabled = (curpage < numberOfpage)
        btnLui.Enabled = (curpage > 1)
        btnDau.Enabled = (curpage > 1)
    End Sub

    Private Sub btnToi_Click(sender As Object, e As EventArgs) Handles btnToi.Click
        Me.Cursor = Cursors.AppStarting
        If isDirty Then
            RaisePageError()
            Return
        End If
        curpage += 1
        lblTrang.Text = String.Format("{0}/{1}", curpage, numberOfpage)
        dgvHLV.DataSource = BuildSearchQuery().Skip((curpage - 1) * pagesize).Take(pagesize)
        SetNavigationState()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnLui_Click(sender As Object, e As EventArgs) Handles btnLui.Click
        Me.Cursor = Cursors.AppStarting
        If isDirty Then
            RaisePageError()
            Return
        End If
        curpage -= 1
        lblTrang.Text = String.Format("{0}/{1}", curpage, numberOfpage)
        dgvHLV.DataSource = BuildSearchQuery().Skip((curpage - 1) * pagesize).Take(pagesize)
        SetNavigationState()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnCuoi_Click(sender As Object, e As EventArgs) Handles btnCuoi.Click
        Me.Cursor = Cursors.AppStarting
        If isDirty Then
            RaisePageError()
            Return
        End If
        curpage = numberOfpage
        lblTrang.Text = String.Format("{0}/{1}", curpage, numberOfpage)
        dgvHLV.DataSource = BuildSearchQuery().Skip((curpage - 1) * pagesize).Take(pagesize)
        SetNavigationState()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnDau_Click(sender As Object, e As EventArgs) Handles btnDau.Click
        Me.Cursor = Cursors.AppStarting
        If isDirty Then
            RaisePageError()
            Return
        End If
        curpage = 1
        lblTrang.Text = String.Format("{0}/{1}", curpage, numberOfpage)
        dgvHLV.DataSource = BuildSearchQuery().Skip((curpage - 1) * pagesize).Take(pagesize)
        SetNavigationState()
        Me.Cursor = Cursors.Default
    End Sub

    Sub RaisePageError()
        MessageBox.Show("Cần thực hiện search trước khi chuyển trang !", "Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End Sub

    Private Sub cbQuocGiaTK_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbQuocGiaTK.SelectedIndexChanged, cbDoiBongTK.SelectedIndexChanged
        Me.Cursor = Cursors.WaitCursor
        isDirty = True
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub txtTK_TextChanged(sender As Object, e As EventArgs) Handles txtTK.TextChanged
        Me.Cursor = Cursors.WaitCursor
        isDirty = True
        Me.Cursor = Cursors.Default
    End Sub
    'Dieu kien luon la 20 CLB


    Private Sub btnThem_Click(sender As Object, e As EventArgs) Handles btnThem.Click
        Me.Cursor = Cursors.WaitCursor
        If txtTen.Text.Length > 0 And cbQuocGia.SelectedValue > 0 Then
            Dim newHLV As New HUANLUYENVIEN With {
            .MaHLV = dgvHLV.RowCount + 1, .TenHLV = txtTen.Text, .NgaySinh = dtNgaySinh.Value,
            .MaQT = cbQuocGia.SelectedValue}

            ctx.HUANLUYENVIENs.InsertOnSubmit(newHLV)
            Try
                ctx.SubmitChanges()
                MessageBox.Show("Thêm thành công !")
            Catch ex As Exception
                MessageBox.Show(ex.Message)

            End Try
        Else
            MessageBox.Show("Điền đầy đủ thông tin trước khi thêm !")
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnDong_Click(sender As Object, e As EventArgs) Handles btnDong.Click
        Me.Cursor = Cursors.WaitCursor
        Close()
        Me.Cursor = Cursors.Default
    End Sub
    Sub CapNhat()
        Dim _HLV As dsHLV = dgvHLV.CurrentRow.DataBoundItem()
        txtTen.Text = _HLV.TenHLV
        lbMaHLV.Text = _HLV.MaHLV
        dtNgaySinh.Value = _HLV.NgaySinh

        Dim HLV As New HUANLUYENVIEN
        HLV = ctx.HUANLUYENVIENs.Single(Function(p) p.MaHLV = _HLV.MaHLV)
        cbQuocGia.SelectedValue = HLV.MaQT
    End Sub
    Private Sub dgvHLV_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvHLV.CellContentClick
        Me.Cursor = Cursors.WaitCursor
        CapNhat()
        Me.Cursor = Cursors.Default
    End Sub
    Private Sub Reload()
        txtTen.Text = String.Empty
        lbMaHLV.Text = String.Empty
        dtNgaySinh.Value = Date.Now()
        cbQuocGia.SelectedIndex = 0
    End Sub

    Private Function KiemTraHLV(ByVal _MaHLV As Integer) As Boolean
        Dim va As New CAULACBO
        va = ctx.CAULACBOs.Single(Function(q) q.MaHLV = _MaHLV)
        If Not va Is Nothing Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Sub Xoa()
        Dim _MaHLV As Integer
        _MaHLV = Integer.Parse(lbMaHLV.Text.ToString())
        If KiemTraHLV(_MaHLV) = True Then
            MessageBox.Show("Huấn Luận Viên Bạn Chọn Không Được Phép Xóa")
            Return
        End If
        Dim _HLV As New HUANLUYENVIEN
        _HLV = ctx.HUANLUYENVIENs.Single(Function(p) p.MaHLV = _MaHLV)
        ctx.HUANLUYENVIENs.DeleteOnSubmit(_HLV)
        ctx.SubmitChanges()
        'dgvHLV.DataSource = hlv.LayDuLieuHLV()06
        Reload()
        MessageBox.Show("Xóa Thành Công")
    End Sub

    Private Sub btnXoa_Click(sender As Object, e As EventArgs) Handles btnXoa.Click
        Me.Cursor = Cursors.WaitCursor
        If lbMaHLV.Text = "" Then
            MessageBox.Show("Chưa Chọn Huấn Luận Viên Muốn Xóa")
            Return
        End If
        If MessageBox.Show("Bạn Có Muốn Xóa Huấn Luyện Viên Đã Chọn", "?", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            Xoa()

        Else
            Return
        End If
        Me.Cursor = Cursors.Default
    End Sub
    Private Sub Sua()
        Dim _MaHLV As Integer
        _MaHLV = Integer.Parse(lbMaHLV.Text.ToString())
        Dim _HLV As New HUANLUYENVIEN
        _HLV = ctx.HUANLUYENVIENs.Single(Function(p) p.MaHLV = _MaHLV)
        _HLV.TenHLV = txtTen.Text
        _HLV.NgaySinh = dtNgaySinh.Value
        _HLV.MaQT = cbQuocGia.SelectedValue
        ctx.SubmitChanges()
        dgvHLV.DataSource = hlv.LayDuLieuHLV()
        Reload()
        MessageBox.Show("Cập Nhật Thành Công")
    End Sub

    Private Sub btnChinhSua_Click(sender As Object, e As EventArgs) Handles btnChinhSua.Click
        Me.Cursor = Cursors.WaitCursor
        If lbMaHLV.Text = "" Then
            MessageBox.Show("Chưa Chọn Huấn Luận Viên Muốn Sửa")
            Return
        End If
        If MessageBox.Show("Bạn Có Muốn Sửa Huấn Luyện Viên Đã Chọn", "?", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            Sua()
        Else
            Return
        End If
        Me.Cursor = Cursors.Default
    End Sub
End Class